package com.region.spring.resolver;

import com.region.http.client.model.MultiFileTransferMeta;
import com.region.http.client.resolver.parameter.AbstractParameterResolver;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 26225
 * @date 2024/7/20 20:40
 * @description
 */
public class MultipartFileParameterResolver extends AbstractParameterResolver {

    @Override
    public Object resolver(Object arg) throws Throwable {
        if (arg instanceof MultipartFile) {
            MultipartFile multipartFile = (MultipartFile) arg;
            return MultiFileTransferMeta
                    .builder()
                    .addInputStream(multipartFile.getName(), multipartFile.getInputStream())
                    .build();
        } else if (arg instanceof MultipartFile[]) {
            MultipartFile[] multipartFiles = (MultipartFile[]) arg;
            MultiFileTransferMeta.MultiFileTransferMetaBuilder builder = MultiFileTransferMeta.builder();
            for (MultipartFile part : multipartFiles) {
                builder.addInputStream(part.getName(), part.getInputStream());
            }
            return builder.build();
        } else {
            return arg;
        }
    }

    @Override
    public int getPriority() {
        return 0;
    }
}
